package com.fitnessapp.database;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class RemoteStorageConnection {
	private static final String TAG = "RemoteStorageConnection";
	
	private static HttpURLConnection connect(String fileUrl){
	
		HttpURLConnection conn = null;
		
		try {
			URL myFileUrl =null; 
			myFileUrl= new URL(fileUrl);			
			conn= (HttpURLConnection)myFileUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			
		} catch (MalformedURLException e) {
			Log.e(TAG + "::connect", e.toString());
		} catch (Exception e) {
			Log.e(TAG + "::connect", e.toString());
		}
		
		return conn;
			
	}
	
	public static Bitmap downloadImage(String fileUrl){
		
		Bitmap bmImg = null;	
		
		HttpURLConnection conn = RemoteStorageConnection.connect(fileUrl);
		
		try {
			InputStream is = null;
			is = conn.getInputStream();
			bmImg = BitmapFactory.decodeStream(is);	
		} catch (IOException e) {
			Log.e(TAG + "::downloadImage", e.toString());
		}		
		
		
		return bmImg;
	}

		
}
